home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / inter33b.zip / INTERRUP.PRI < prev    next >
Text File  |  1992-09-13  |  5KB  |  103 lines

  1.                      iAPX 86 Interrupt Primer
  2.                      ------------------------
  3.  
  4.                           by Ralf Brown
  5.                               12/87
  6.              Updated 6/88, 4/90, 9/92
  7.  
  8.  
  9. What is an interrupt?
  10.    An interrupt is a hardware signal that tells the CPU to 
  11.    temporarily stop what it is doing and go do something else.  
  12.    Without interrupts, the CPU would have to constantly check for 
  13.    external events; with interrupts, the CPU can work on 
  14.    something else and still respond to an event as soon as it 
  15.    occurs. 
  16.  
  17.    CPUs typically have an instruction to disable interrupts for 
  18.    use when a section of code has to run without being disturbed 
  19.    by external events.  Because of this, most CPUs also have a 
  20.    special interrupt called a Non-Maskable Interrupt (NMI), which 
  21.    is responded to even when all other interrupts are disabled.  
  22.    The NMI is used to signal calamities such as memory failure or 
  23.    imminent power loss.
  24.  
  25. Why so many different interrupts?
  26.    The 8086 family of processors has the ability to recognize 256 
  27.    different interrupts.  They also have the ability to let a 
  28.    program invoke any of these interrupts with a special 
  29.    instruction, known as a software interrupt (as opposed to a 
  30.    hardware interrupt which is signalled from outside the 
  31.    processor).  Software interrupts are treated just like 
  32.    hardware interrupts, except that they are never disabled and 
  33.    do not result in an acknowledgement to other chips in the 
  34.    computer.  The software interrupt instruction on the 8086 
  35.    family is called INT, and is given the number of the 
  36.    interrupt.  Thus an INT 21h instruction invokes interrupt 
  37.    number 33 decimal.
  38.  
  39.    Other processors also have software interrupts, though they 
  40.    often use different names, such as the Motorola 68000 family 
  41.    TRAP instruction, the Intel 8080 RST (ReSTart) instruction, or
  42.    many mainframes' SVC (SuperVisor Call). 
  43.  
  44.    Since a program can invoke an interrupt by number rather than 
  45.    by its address (as it has to in calling subroutines), 
  46.    interrupts are a convenient way of providing services without 
  47.    having to recompile a program whenever the address of the code 
  48.    providing the service changes.  This also allows a user 
  49.    program to enhance the services provided by directing the 
  50.    interrupt to itself.  These enhanced services can then be made 
  51.    available to other programs.
  52.  
  53. How does an interrupt work?
  54.    The 8086 reserves the lowest 1024 bytes of memory for a table 
  55.    containing the addresses for each of the 256 possible 
  56.    interrupts.  When an interrupt occurs (hardware or software), 
  57.    the processor multiplies its number by 4 and looks at the 
  58.    resulting memory location to find the address of the piece of 
  59.    code which handles the interrupt.  It then places the current 
  60.    address in the program and the processor flags on the stack, 
  61.    and jumps to the beginning of the interrupt handler.
  62.  
  63.    When the interrupt handler finishes, it invokes a special 
  64.    instruction to return from the interrupt.  This instruction 
  65.    takes the previously saved flags and program address off of 
  66.    the stack and places them back in the appropriate registers in 
  67.    the CPU.
  68.  
  69.    The interrupt handler has to be careful to preserve any 
  70.    registers that it uses which are not used to communicate 
  71.    results to the program that invoked the interrupt.  If the 
  72.    interrupt can be triggered by a hardware interrupt (only 
  73.    certain ones can on IBM PC's, XT's, and AT's), then the 
  74.    interrupt handler has to preserve ALL registers, since the 
  75.    interrupt could have happened anywhere.
  76.  
  77.  
  78. GLOSSARY
  79. --------
  80. API (Application Program[ming] Interface)
  81.    An API is the set of function calls and services that a
  82.    program makes available to other processes (applications).
  83.    Each function or service has a set format which specifies
  84.    the values to be supplied by the caller and the values
  85.    which are returned.    Because of this interface
  86.    specification, the underlying organization of the
  87.    function or service can be changed without affecting the
  88.    applications which use it.  For example, the DOS INT 21h
  89.    file access functions remained unchanged between DOS 2.x
  90.    and DOS 3.x, even though the internal data structures and
  91.    code organization changed significantly.
  92.  
  93. NMI (Non-Maskable Interrupt)
  94.    Most external (hardware) interrupts can be disabled by the
  95.    CLI (CLear Interrupt enable flag) instruction when the CPU
  96.    is executing critical code that should not be interrupted,
  97.    such as switching from one stack to another.  However, there
  98.    are some situations so dire that the CPU must act on them
  99.    immediately no matter what else it is doing, even if it has
  100.    disabled interrupts.  The Non-Maskable Interrupt serves
  101.    precisely this purpose, as it cannot be disabled (masked) by
  102.    the CPU.
  103.